package com.yongcheon.sms_manager.receiver.phone;

import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.telephony.ITelephony;
import com.yongcheon.sms_manager.data.phone_spsm.PhoneSpamData;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.notification.SpamPhoneNotification;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;
import com.yongcheon.sms_manager.vo.SpamNumVO;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-12-26.
 */
public class PhoneListener extends PhoneStateListener {
    private static int previousState = TelephonyManager.CALL_STATE_IDLE;
    private final String TAG = "PhoneListener";
    Context context;

    public PhoneListener(Context context){
        this.context = context;
    }

    public void onCallStateChanged(int currentState, String incomingNumber){
        if(previousState != currentState) {
            previousState = currentState;
            SpamNumVO spamNumVO = getSpamInfo(incomingNumber);
            switch (currentState) {
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.i(this.TAG, "PhoneListener->onCallStateChanged() -> CALL_STATE_IDLE ");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.i(this.TAG, "PhoneListener->onCallStateChanged() -> CALL_STATE_OFFHOOK ");
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.i(this.TAG, "PhoneListener->onCallStateChanged() -> CALL_STATE_RINGING ");
                    if(spamNumVO != null) {
                        if(DefaultAppUtil.isEnblePhoneBlick(context)){
                            Log.i(this.TAG, "End call");
                            endCall();
                            setNoti(spamNumVO);
                        }else{
                            Log.i(this.TAG, "Toast msg");
                            startThread(spamNumVO);
//                            setNoSound();
                        }
                    }
                    break;
                default:
                    Log.i(this.TAG, "PhoneListener->onCallStateChanged() -> default -> " + Integer.toString(previousState));
                    break;
            }
            if(spamNumVO != null) insertSpam(spamNumVO);
        }
    }

    public void setNoSound(){
        AudioManager aManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    }

    public void insertSpam(SpamNumVO spamNumVO){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        PhoneSpamData.registAllSpam(context, spamNumVO);
    }

    public void setNoti(SpamNumVO spamNumVO){
        SpamPhoneNotification spamPhoneNotification = new SpamPhoneNotification();
        spamPhoneNotification.setNotification(context.getApplicationContext(), spamNumVO);
    }

    public void endCall(){
        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Class c = Class.forName(telephony.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            ITelephony telephonyService = (ITelephony) m.invoke(telephony);
            //telephonyService.silenceRinger();
            telephonyService.endCall();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void startThread(SpamNumVO spamNumVO){
        PhoneHandler phoneHandler = new PhoneHandler(context, spamNumVO);
        NewsThread2 thread = new NewsThread2(phoneHandler);
        thread.setSpamNumVO(spamNumVO);
        thread.start();
    }

    public SpamNumVO  getSpamInfo(String num){
        SpamNumVO spamNumVO = null;
        SpamNumberData spamNumberData = new SpamNumberData(context);
        ArrayList<SpamNumVO> list =  spamNumberData.selectReturnVo(num, context, SpamDBManager.getDatabase(context));
        if(list.size() > 0) spamNumVO = list.get(0);
        return spamNumVO;
    }

    class NewsThread2 extends Thread {
        Handler mHandler;
        SpamNumVO spamNumVO;

        public NewsThread2(Handler handler) {
            mHandler = handler;
        }

        public void setSpamNumVO(SpamNumVO spamNumVO){
            spamNumVO = spamNumVO;
        }
        public void run() {
            while(previousState == TelephonyManager.CALL_STATE_RINGING) {
                Message msg = new Message();
                msg.what = 0;
                msg.obj = spamNumVO;
                mHandler.sendMessage(msg);
                try { Thread.sleep(3500);} catch (Exception e) { ; }
            }
        }
    }

}
